package com.baosight.sczx.common.zm.cInterface.cp;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanJJ;
import com.baosight.sczx.zm.cs.domain.ZMCSRoadWay;

public class ProducePlanJJ {

	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	
	/**
	 * 验证是否存在记录
	 * @param params
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static boolean validate(Map<String, String> params) {
		if (params == null) {
			return true;
		}
		
		List result = dao.query("ZMCPProducePlanJJ.validate", params);
		if(result != null && result.size() > 0) {
			return true;
		}
		return false;
	}
	
	/**
	 * 从采煤计划明细表中
	 * 获得专业采煤计划在上报的时候的统计值
	 * 原煤产量、保护层面积、厚保面积比、原煤灰份
	 * ‘回采工效’暂不填写
	 * 原煤产量 = 容重 * 采长 * 采高 * 推进
	 * @param fdProfessionalplanId
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static Map<String, BigDecimal> JJModify(String fdProfessionalplanId, String fdProduceplanId) {
		if(fdProfessionalplanId == null && fdProduceplanId == null) {
			return null;
		}
		
		Map<String, String> _params = new HashMap<String, String>();
		_params.put("fdProfessionalplanId", fdProfessionalplanId);
		_params.put("fdProduceplanId", fdProduceplanId);
		
		List result = dao.query("ZMCPProducePlanJJ.query", _params);
		
		Map<String, BigDecimal> _result = new HashMap<String, BigDecimal>();
		
		Double fdPlanSum = new Double(0); //计划掘进进尺
		
		for (int i = 0; i < result.size(); i++) {
			ZMCPProducePlanJJ plan = (ZMCPProducePlanJJ)result.get(i);
			fdPlanSum += plan.getFdPlanSum() == null ? 0 : plan.getFdPlanSum().doubleValue();//计划掘进进尺
		}
		
		_result.put("jcSum", BigDecimal.valueOf(fdPlanSum));
		return _result;
	}
	
	/**
	 * 专业计划上报成功
	 * 修改掘进明细的 生产计划字段
	 * 复制计划
	 */
	@SuppressWarnings("rawtypes")
	public static void cloneJJ(String fdProduceplanId, String fdProfessionalplanId) {
		if(fdProduceplanId != null && !fdProduceplanId.trim().equals("") && fdProfessionalplanId != null && !fdProfessionalplanId.trim().equals("")) {
			Map<String, String> _map = new HashMap<String, String>();
			_map.put("fdProfessionalplanId", fdProfessionalplanId);
			
			List result = dao.query("ZMCPProducePlanJJ.query", _map);
			for(int i = 0; i < result.size(); i ++) {
				ZMCPProducePlanJJ _plan = (ZMCPProducePlanJJ)result.get(i);
				String fdKeyId = TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN_JJ");
				_plan.setFdKeyId(fdKeyId);
				_plan.setFdProduceplanId(fdProduceplanId);

				dao.insert("ZMCPProducePlanJJ.insert", _plan.toMap());
			}
		}
	}
	
	/**
	 * 生产计划调整时，复制掘进计划
	 * @param fdProduceplanId
	 */
	@SuppressWarnings("rawtypes")
	public static void cloneByAdjust(String oldId, String newId) {
		if(oldId != null && newId != null) {
			Map<String, String> _map = new HashMap<String, String>();
			_map.put("fdProduceplanId", oldId);
			
			List result = dao.query("ZMCPProducePlanJJ.query", _map);
			for(int i = 0; i < result.size(); i ++) {
				ZMCPProducePlanJJ _plan = (ZMCPProducePlanJJ)result.get(i);
				String fdKeyId = TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN_JJ");
				_plan.setFdKeyId(fdKeyId);
				_plan.setFdProduceplanId(newId);
				_plan.setFdProfessionalplanId("");
				
				dao.insert("ZMCPProducePlanJJ.insert", _plan.toMap());
			}
		}
	}
	
	/**
	 * 掘进计划列表，供调度验收使用
	 * @param blockName
	 * @param fdMiningId
	 * @param fdPlanYear
	 * @param fdPlanMonth
	 * @param isAll
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static EiBlock getRoadWayBlock(String blockName, String fdMiningId, String fdPlanYear, String fdPlanMonth, String fdDepartmentId, boolean isAll){                                                              
		List<Map>  _list = new ArrayList<Map>();
		blockName = blockName == null ? "cmes.cp.roadway" : blockName;
		
		EiBlock block = new EiBlock(blockName);
		block.addBlockMeta(new ZMCSRoadWay().eiMetadata);
		
		EiColumn eiColumn = new EiColumn("value");
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		eiColumn = new EiColumn("display");
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		eiColumn = new EiColumn("fdDepartmentId");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdMiningId", fdMiningId);
		params.put("fdPlanYear", fdPlanYear);
		params.put("fdPlanMonth", fdPlanMonth);
		params.put("fdDepartmentId", fdDepartmentId);
		
		if(isAll) {
			Map<String, String> _map = new HashMap<String, String>();
			_map.put("value", "");
			_map.put("display", "");
			_list.add(_map);

			List<Map> _temp = dao.query("ZMCPProducePlanJJ.queryInterface", params);
			for(int i = 0; i < _temp.size(); i++) {
				_list.add(_temp.get(i));
			}
		}
		else {
			_list = dao.query("ZMCPProducePlanJJ.queryInterface", params);
		}
		
		block.setRows(_list);
		return block;
	}
	
	/**
	 * 专业计划退回时，删除数据
	 * @param oldId
	 * @param newId
	 */
	public static int goback(String fdProfessionalplanId) {
		if(fdProfessionalplanId != null) {
			Map<String, String> _map = new HashMap<String, String>();
			_map.put("fdProfessionalplanId", fdProfessionalplanId);
			return dao.delete("ZMCPProducePlanJJ.goback", _map);
		}
		return -1;
	}
}
